home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 20 / macformat_20.iso / mac / Shareware / Comunicaciones / PPP_PrefSaver 1.2 / C Source / PPP-Prefs.c < prev    next >
C/C++ Source or Header  |  1996-05-09  |  5KB  |  197 lines

  1. /********************************************************************************************
  2.  * PPP-Prefs.c                                                                                *
  3.  *                                                                                            *
  4.  *        These routines were designed to handle the preferences for MacPPP.                    *
  5.  *        [Originally Assembled] by Tony Andreoli, 12/5/95                                    *
  6.  *        [Modified by Eric Long 4/96 to use as an IEnd with Installer Maker]                                                        *
  7.  *                                                                                            *
  8.  *        You may use these routines freely, I only ask that you credit me somewhere in your    *
  9.  *        code.                                                                                *
  10.  *                                                                                            *
  11.  *    Notes:                                                                                    *
  12.  *        • HandleError is just a simple error handler.                                        *
  13.  *        • OpenPPP_Prefs simply opens the PPP Preferences file, and returns the fRef.        *
  14.  *        • MacPPP considers the PREF the first part of the data fork, which consists of        *
  15.  *          settings that do not change across sets.  It considers a CONFIG those settings    *
  16.  *          specific to a set.                                                                *
  17.  *        • The config index starts at 0, yet thePref.max_config starts at 1.  Thus if         *
  18.  *          thePref.max_config=4, the last config is 3 (0..3).                                *
  19.  *        • Be sure to look in the PPP-Prefs.h file for the structure of the prefs and        *
  20.  *          config.                                                                            *
  21.  *        • Each routine opens then closes the PPP Preferences file.  It is NOT left open        *
  22.  *          upon return.                                                                        *
  23.  *                                                                                            *
  24.  *    Purpose:                                                                                *
  25.  *        GetPPP_Pref - Gets the PPP preferences.                                                *
  26.  *        GetPPP_Config - Gets a specified config set.                                        *
  27.  *        PutPPP_Config - Writes config to a specified set.                                    *
  28.  *                                                                                            *
  29.  ********************************************************************************************/
  30.  
  31. #include "PPP-Prefs.h"
  32. #define thePrefsFile "\pPPP Preferences"
  33.  
  34. /* Routines added to link with Fetch_Settings.c */
  35. #if STANDALONE
  36. void            ToolBoxInit ( void );
  37. #endif
  38. Boolean            GetUserPrefs( struct ppp_config    *theConfig, OSErr *err );
  39.  
  40. /* main() and other routines have been modified in various ways to suit
  41. the purposes of this project */
  42.  
  43.  
  44. /************************ main ***************************************/
  45.  
  46. #if STANDALONE
  47. void    main( void )
  48. #else
  49. pascal short main(short abort,short vol,long dir,uchar *name,
  50.                   unsigned short packages)
  51. #endif              
  52. {
  53.     struct    ppp_pref thePref;
  54.     struct    ppp_config theConfig;
  55.     OSErr    err = 0;
  56.  
  57. #if STANDALONE
  58.     ToolBoxInit();
  59. #else
  60.     if (abort) { goto out;}
  61. #endif
  62.         
  63.     
  64.     err = GetPPP_Pref(&thePref);
  65.         // If err == file not found, the PPP Prefs file isn't installed so we'll do nothing.
  66.     if (! err)
  67.         err = GetPPP_Config(&theConfig,thePref.active_config);
  68.         
  69.     if (!err){
  70.         if (GetUserPrefs(&theConfig, &err))
  71.             PutPPP_Config(theConfig, thePref.active_config);
  72.         else{
  73.             if (err)
  74.                 HandleError(err);
  75.         }
  76.     }
  77. #if (! STANDALONE)
  78. out:
  79.     return (abort);
  80. #endif
  81. }
  82.  
  83.  
  84.  
  85. /************************* ToolBoxInit ******************************/
  86. #if STANDALONE
  87. void        ToolBoxInit ( void )
  88. {
  89.     InitGraf ( &thePort );
  90.     InitFonts ();
  91.     InitWindows ();
  92.     InitMenus ();
  93.     TEInit ();
  94.     InitDialogs ( 0L );
  95.     InitCursor ();
  96. }
  97. #endif
  98.  
  99. /************************ HandleError *******************************/
  100.  
  101. void HandleError(OSErr errNum)
  102. {
  103.     Str255    theErr;
  104.     
  105.     if (errNum!=noErr) {
  106.         NumToString(errNum,theErr);
  107.         ParamText(theErr,"\p","\p","\p");
  108.         Alert(rErrorAlert,nil);
  109.     }
  110. }
  111.  
  112.  
  113. /**************************** OpenPPP_Prefs *****************************/
  114.  
  115. short OpenPPP_Prefs(OSErr *err)
  116. {
  117.     short    foundvRefNum;
  118.     long    foundDirID;
  119.     short    SystemFolderID;
  120.     short    theResFile;
  121.     FInfo    thefInfo;
  122.     
  123.     *err=FindFolder(kOnSystemDisk,kPreferencesFolderType,kDontCreateFolder,&foundvRefNum,&foundDirID);    /* Get location of the preferences folder    */
  124.     *err = HOpen(foundvRefNum,foundDirID,thePrefsFile,fsRdWrShPerm,&theResFile);
  125.     if ((*err != noErr) && (*err != fnfErr)) {    // If we can't find it, no alert, just get out.
  126.         HandleError(*err);
  127.     }
  128.     return theResFile;
  129. }
  130.  
  131.  
  132. /************************* GetPPP_Pref ********************************/
  133.  
  134. OSErr GetPPP_Pref(struct ppp_pref *thePref)
  135. {
  136.     long    count;
  137.     OSErr    rc = noErr;
  138.     short    prefref;
  139.     Str255    theStr;
  140.  
  141.     prefref=OpenPPP_Prefs(&rc);
  142.     
  143.     if (! rc){
  144.         rc = SetFPos(prefref,fsFromStart,0);
  145.         count = sizeof ( struct ppp_pref );
  146.         if ( rc == noErr) {
  147.             rc = FSRead(prefref, &count, thePref);
  148.         }
  149.         else
  150.             HandleError(rc);
  151.         FSClose(prefref);
  152.     }
  153.     return(rc);
  154. }
  155.  
  156.  
  157. /************************** GetPPP_Config **********************************/
  158.  
  159. OSErr GetPPP_Config(struct ppp_config *theConfig,short confignum)
  160. {
  161.     long    count;
  162.     OSErr    rc;
  163.     short    prefref;
  164.  
  165.     prefref=OpenPPP_Prefs(&rc);
  166.     rc = SetFPos(prefref,fsFromStart,sizeof (struct ppp_pref)+(sizeof (struct ppp_config) * confignum));
  167.     count = sizeof ( struct ppp_config );
  168.     if ( rc == noErr) {
  169.         rc = FSRead(prefref, &count, theConfig);
  170.     }
  171.     else
  172.         HandleError(rc);
  173.     FSClose(prefref);
  174.  
  175.     return(rc);
  176. }
  177.  
  178.  
  179. /*********************** PutPPP_Config ********************************/
  180.  
  181. void PutPPP_Config(struct ppp_config theConfig,short confignum)
  182. {
  183.     long    count;
  184.     OSErr    rc;
  185.     short    prefref;
  186.  
  187.     prefref=OpenPPP_Prefs(&rc);
  188.     rc = SetFPos(prefref,fsFromStart,sizeof (struct ppp_pref)+(sizeof (struct ppp_config) * confignum));
  189.     count = sizeof ( struct ppp_config );
  190.     if ( rc == noErr) {
  191.         rc = FSWrite(prefref, &count, &theConfig);
  192.     }
  193.     else
  194.         HandleError(rc);
  195.     FSClose(prefref);
  196.     
  197. }